<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\user\controller;

use cmf\controller\RestBaseController;
use think\Db;

class UserController extends RestBaseController
{

    public function userInfo()
    {
        $user_id = $this->getUserId();

        $user = Db::name("user")->field("id, post_name, post_sex, post_phone, department_id, department_name, position_id, position_name, post_status")->where("id", $user_id)->find();

        $department = Db::name("department_lists")->where("status", 1)->select()->toArray();
        $position = Db::name("position_lists")->where("status", 1)->select()->toArray();

        $status_memo = [0 => "未认证", 1 => "已认证", 2 => "待审核", 3 => "认证未通过"];

        return $this->success("获取成功", ['user' => $user, "status_memo" => $status_memo, "department" => $department, "position" => $position]);
    }

    public function postInfo()
    {
        $param = $this->request->param();

        if (empty($param['post_name'])) {
            return $this->error("请输入姓名");
        }
        if (empty($param['post_phone'])) {
            return $this->error("请输入电话号码");
        }

        $user_id = $this->getUserId();
        $user = Db::name("user")->where("id", $user_id)->find();
        if (empty($user)) {
            return $this->error("获取用户信息失败，请刷新重试");
        }
        if ($user['post_status'] == 1) {
            return $this->error("已认证");
        }
        if ($user['post_status'] == 2) {
            return $this->error("待审核");
        }

        Db::name("user")->where("id", $user_id)->update($param);

        return $this->success("提交成功");
    }

}
